package com.shipwe.loans.utils.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.shipwe.loans.R
import com.shipwe.loans.databinding.DialogCheckNumberBirthdayBinding
import com.shipwe.loans.http.HttpHelper
import com.shipwe.loans.pro.ShipweDialog
import com.shipwe.loans.utils.ShipweToastManager
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventPointType
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/15 10:29
 * @Annotation：检验手机号选择生日Dialog
 */
class CheckNumberBirthdayDialog(context: Context) : ShipweDialog(context) {
    lateinit var binding: DialogCheckNumberBirthdayBinding
    private var callback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    //出生日期选择
    private var birthdayDialog: BirthdayDialog? = null
    //选择的生日
    private var selectBirthday: JSONObject? = null
    override fun getRootView(): View {
        binding = DialogCheckNumberBirthdayBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(binding.checkNumberBirthdayViewGroup)
        setOnClickListener(binding.checkNumberBirthdayConfirm)
        setOnClickListener(binding.checkNumberBirthdayClose)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.checkNumberBirthdayClose -> {//关闭
                dismiss()
            }
            R.id.checkNumberBirthdayViewGroup -> {//选择生日
                showCheckNumberBirthDialog()
            }
            R.id.checkNumberBirthdayConfirm -> {//提交
                if (selectBirthday == null){
                    ShipweToastManager.showShortText(context, context.getString(R.string.check_number_birthday_hint))
                    return
                }
                if (callback != null){
                    callback?.onSubmitSelectBirthday(selectBirthday!!)
                    dismiss()
                }
            }
        }
    }

    /**
     * 事件备注
     */
    fun setCheckNumberBirthdayEventRemark(remark: String): CheckNumberBirthdayDialog{
        this.eventRemark = remark
        return this
    }


    /**
     * 设置监听回调
     */
    fun setCheckNumberBirthdayClickCallback(callback: IDialogCallback): CheckNumberBirthdayDialog{
        this.callback = callback
        return this
    }

    fun setCheckNumberBirthdayCancelable(flag: Boolean): CheckNumberBirthdayDialog{
        setCancelable(flag)
        return this
    }

    fun setCheckNumberBirthdayCanceledOnTouchOutside(flag: Boolean): CheckNumberBirthdayDialog{
        setCanceledOnTouchOutside(flag)
        return this
    }

    /**
     * 选择出生日期
     */
    private fun showCheckNumberBirthDialog(){
        if (birthdayDialog == null){
            birthdayDialog = BirthdayDialog(context,"",object : BirthdayDialog.IDialogCallback{
                override fun completed(selectDate: JSONObject) {
                    //可根据需要自行截取数据显示
//                    val obj = JSONObject()
//                    obj.put("title",selectDate.optString("showDate"))
//                    obj.put("title_sw",selectDate.optString("showDate"))
//                    obj.put("value",selectDate.optString("subDate"))
                    selectBirthday = selectDate
                    binding.checkNumberBirthday.text = selectDate.optString("showDate")
                }
            })
        }
        birthdayDialog!!.show()
    }

    interface IDialogCallback {
        //提交
        fun onSubmitSelectBirthday(data: JSONObject)
    }

    override fun onStart() {
        super.onStart()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_CHECK_NUMBER_BIRTHDAY_SHOW, eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.cancelTag(this)
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_CHECK_NUMBER_BIRTHDAY_DISMISS, eventRemark)
    }
}